<?php
class TradeDetail extends ActiveRecord
{
	public $id_tradedetail;
	public $id_merchant;
	public $amount_due;
	public $amount;
	public $discount;
	public $mchreturn;
	public $out_tradeno; 
	public $channel_tradeno; 
	public $trade_status;
	public $allpay_biz_channel;
	public $allpay_biz_type;
	public $allpay_merchantno;
	public $allpay_terminalno;
	public $id_wxuser;
	public $createtime;	
	public $paytime;
	public $feerate;
	public $fee;
    public $merchant_name;
    public $is_subscribe;
    public $is_get_redpack;
    public $get_redpack_type;
    public $get_redpack_desc;
    public $notify_url;
    public $id_newretail_user;
    public $id_newretail_merchant;

	const BIZ_CHANNEL_ALIPAY = 'ali';
	const BIZ_CHANNEL_WXPAY = 'wx';

    const BIZ_TYPE_WX_PREPAY = 'wx.prepay';					// 扫码-预下单
    const BIZ_TYPE_WX_MICROPAY = 'wx.tradepay';				// 条码支付
    const BIZ_TYPE_WX_JSPAY = 'wx.jspay';					// JSAPI线上支付

    const BIZ_TYPE_ALI_JSPAY = 'ali.jspay';					// 支付宝JSAPI线上支付

	const TRADE_STATUS_STARTUP = '0';
	const TRADE_STATUS_SUCCESS = '1';
	const TRADE_STATUS_FAILED = '2';
	const TRADE_STATUS_CANCELED = '3';
	const TRADE_STATUS_REFUND = '4';
	const TRADE_STATUS_REVERSE = '5';

    //每页默认条数
    const default_limit = 20;
    //同一商户领取时间限制(秒)
    const LIMI_TIME = 3600;
    //限制领取的次数
    const LIMI_GET_TIMES = 2;
    //可以红包
    const CAN_GET_REDPACK = 1;
    //不可领红包
    const CANNOT_GET_REDPACK = 2;
    //可领红包金额限制(分)
    const LIMIT_AMOUNT = 100;
    //关注公众号
    const SUBSCRIBE = '1';
    //已领取
    const GET_REDPACK = 1;
    const NOT_GET_REDPACK = 0;

    const SECOND_REQUEST_NO = '2';

    const GET_REDPACK_TYPE1 = '1';
    const GET_REDPACK_TYPE2 = '2';
    const GET_REDPACK_TYPE3 = '3';
    const GET_REDPACK_TYPE4 = '4';
    const GET_REDPACK_TYPE5 = '5';
    const GET_REDPACK_TYPE6 = '6';

    const GET_REDPACK_DESC = array(
        '1' => '正常',
        '2' => '非第一笔交易未关注摇钱吧',
        '3' => '订单金额未达标',
        '4' => '同一商户超过规定时间领取次数',
        '5' => '已有所有公众号的关注记录',
        '6' => '已发放双倍卡',
    );

    public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function tableName()
	{
		return 'tradedetail';
	}
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('feerate, id_merchant, fee, id_tradedetail, amount, amount_due, discount, mchreturn, out_tradeno, channel_tradeno, trade_status, allpay_biz_channel, allpay_biz_type, allpay_merchantno, allpay_terminalno, id_wxuser, createtime', 'safe'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			
		);
	}
	
	public function getAttributes($names = null)
	{
		$attributes = parent::getAttributes($names);
		
		
		return $attributes;
	}

	public static function getDescByType($type)
    {
        $limit = self::LIMIT_AMOUNT / 100;
        $time = self::LIMI_TIME / 3600;
        $descs = array(
            '1' => '正常',
            '2' => '亲，您还未关注摇钱吧福利社哦，下次支付前记得关注呢',
            '3' => '亲，订单金额满'.$limit.'元才能给您发红包哦',
            '4' => '亲，同一商户'.$time.'小时内只能领取'.self::LIMI_GET_TIMES.'次哦',
            '5' => '亲，感谢您对我们的支持',
        );

        if(key_exists($type,$descs)){
            return $descs[$type];
        }
        return '';
    }

	public function genOuttradeno()
	{
	    return
	    (rand(100000, 999999) . rand(100000, 999999)) +
	    date('ymdHis') +
	    (
	        str_pad('', 6, 0, STR_PAD_LEFT) .
	        str_pad($this->id_wxuser, 6, 0, STR_PAD_LEFT)
	    );
	}
	
	public function getFormat1CDate()
	{
		$now = time();
		$createtime = strtotime($this->createtime);
		
		$diff = $now - $createtime; 
		$diff = $diff < 0? 0 : $diff;
		
		if($diff < 60){
			return intval($diff) . '秒前';
		}else if($diff < 60*60){
			return intval($diff/60) . '分钟前';
		}else if($diff < 60*60*24){
			return intval($diff/60/60) . '小时前';
		}else{
			return intval($diff/60/60/24) . '天前';
		}
	}
	
	public function getFormat2CDate()
	{
		$now = time();
		$createtime = strtotime($this->createtime);

		if(date('Y', $now) == date('Y', $createtime)){
			if(date('Y-m-d', strtotime('-1 day')) == date('Y-m-d', $createtime)){
				return '昨天' . date('H:i', $this->createtime);
			}else if(date('Y-m-d', $now) == date('Y-m-d', $$createtime)){
				return date('今天H:i:s', $createtime);
			}else{
				//return date('m月d日 H:i', $createtime);
				return date('m月d日 H:i', $createtime);
			}
		}else{
			//return date('Y年m月d日 H:i', $createtime);
			return date('Y年m月d日  H:i', $createtime);
		}
	}

	public function getTradeByUid($wxuser_id,$offset=0,$limit=10)
    {
        $trades = $this->findAllBySql('
                SELECT t.id_tradedetail,t.get_redpack_type,t.is_subscribe,t.is_get_redpack, t.id_merchant, t.amount_due, t.discount, t.amount, t.createtime, merchant.name merchant_name 
	            FROM tradedetail t  
	            LEFT JOIN merchant ON t.id_merchant = merchant.id_merchant
	            WHERE t.id_wxuser = :id_wxuser AND t.trade_status = :trade_status
                ORDER BY createtime DESC
                Limit :offset,:limit
            ', array(
            ':id_wxuser' => $wxuser_id,//$this->user->id_wxuser
            ':offset' => $offset,
            ':limit' => $limit,
            ':trade_status'=>'1'
        ));

        $trades = empty($trades)?[]:$trades;

        /*$first = [];
        $sec = [];
        $thr = [];
        foreach ($trades as $k =>$trade){
            if($trade->is_subscribe == '1' && $trade->is_get_redpack== '0' && $trade->amount_due >= 100){
                $first[$k] = $trade;
            }elseif ($trade->is_get_redpack == '1'){
                $sec[$k] = $trade;
            }else{
                $thr[$k] = $trade;
            }
        }
        foreach ($sec as $item){
            $first[] = $item;
        }
        foreach ($thr as $value){
            $first[] = $value;
        }*/
        return $trades;

    }
	
}
